package com.jinzhuo.music.service;

import com.jinzhuo.music.dao.RankMapper;
import com.jinzhuo.music.domain.Rank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RankServiceImpl implements RankService {
    @Autowired
    private RankMapper rankMapper;
    /**
     * 添加
     * @param pojo
     */
    @Override
    public boolean addRank(Rank pojo) {
      return rankMapper.addRank(pojo)>0;
    }

    /**
     * 根据歌单id查总分
     *
     * @param songListId
     * @return
     */
    @Override
    public int getScoreSum(Integer songListId) {
        return rankMapper.getScoreSum(songListId);
    }

    /**
     * 查歌单的总评分人数
     *
     * @param songListId
     * @return
     */
    @Override
    public int getTotal(Integer songListId) {
        return rankMapper.getTotal(songListId);
    }

    /**
     * 计算歌单平均分
     *
     * @param songListId
     * @return
     */
    @Override
    public int getAvgScore(Integer songListId) {
        int scoreSum = rankMapper.getScoreSum(songListId);
        int total = rankMapper.getTotal(songListId);
        if (total==0){
            return 1; //没有打分则默认是一分
        }else {
        int average = scoreSum/total;
        return average;
        }
    }
}
